<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class setup extends CI_Controller{
    function __construct() {
        parent::__construct();
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("If-Modified-Since: Mon, 22 Jan 2008 00:00:00 GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0", false);
	header("Cache-Control: private");
        header("Pragma: no-cache");
	$this->auth	= unserialize(base64_decode($this->session->userdata('sisfodealer')));
        $this->load->model(array('mdealer','msetup'));
    }
    function index(){
        if($this->auth){
            $data['module'] = "master";
            $this->load->view('home', $data);
	}else{
            $data['pesan'] = "<font color='red'>anda harus login!</font>";
            $this->load->view('login',$data);
            return;
	}
    }
    function getdisplay($param=""){
        $data['type'] = $param;
        $this->load->view("setup/setup_mng",$data);
    }
    function gettab(){
		$type = $this->input->post('type');
		switch($type){
			case 'user_list':
                                //$data['idx_user_group']  = $this->combobox('idx_user_group','', 'return');
				
			break;
			case 'user_level':
				
			break;
			case 'user_group':
				
			break;
			case 'user_role':
				
			break;
			case 'app_modules':
				
			break;
			case 'app_functions':
				
			break;
					
		}
		$data['type'] = $type;
		$this->load->view('setup/setup_tab',$data);
	}
        function getform($editstatus="", $type=""){
            $data=array();
                if($editstatus == 'edit'){
			$id = $this->input->post('id');
			$datarecord = $this->msetup->getData($type, $id);
			$data['datarecord'] = $datarecord;
                        $data['idx_user_member']      = $this->combobox('idx_user_member','', 'return');
                        //print_r($datarecord);
		}
		switch($type){
			case 'user_list':
				$data['idx_user_group']     = $this->combobox('idx_user_group',($editstatus == 'edit' ? $datarecord['group_user_id'] : ''), 'return');
                                $data['idx_user_subgroup']  = $this->combobox('idx_user_subgroup',($editstatus == 'edit' ? $datarecord['sub_group_id']: ''), 'return');
                                $data['idx_user_level']     = $this->combobox('idx_user_level',($editstatus == 'edit' ? $datarecord['user_level_id']: ''), 'return');
                                $data['idx_user_role']      = $this->combobox('idx_user_role','', 'return');
                        break;
			case 'app_functions':
				$data['module']             = $this->combobox('tbl_user_modules',($editstatus == 'edit' ? $datarecord['tbl_module_id'] : ''), 'return');
                        break;
			
		}
		
		$data['tinggiregion'] 	= $this->input->post('tinggiregion');
		$data['editstatus'] 	= $editstatus;
		$data['type']		= $type;
		$this->load->view('setup/setup_form', $data);
	}
        
        function policy($id){
            $data['tinggiregion'] 	= $this->input->post('tinggiregion');
            $data['id']=$id;
            $this->load->view("setup/policy",$data);
        }
        function savePolicy(){
            $post= $this->input->post("dom_checked");
            $role = $this->input->post("role_id");
            $this->db->delete("tbl_user_policy",array("idx_role_id"=>$role));
            $data = array(); 
            $exp="";
            //$this->db->trans_begin();
            foreach ($post as $value) {
                $exp=  explode("_", $value);
                $data["idx_role_id"]=$exp[2];
                $data["tbl_module_id"]=$exp[4];
                $data["tbl_function_id"]=$exp[6];
                $data["is_create"]=0;
                $data["is_read"]=0;
                $data["is_update"]=0;
                $data["is_delete"]=0;
                //print_r($exp[0]);
                switch($exp[0]){
                    case "C":
                        $data["is_create"]=1;
                        break;
                    case "R":
                        $data["is_read"]=1;
                        break;
                    case "U":
                        $data["is_update"]=1;
                        break;
                    case "D":
                        $data["is_delete"]=1;
                        break;
               }
               
               $cek=$this->db->get_where("tbl_user_policy",array("tbl_function_id"=>$data["tbl_function_id"],"tbl_module_id"=>$data["tbl_module_id"],"idx_role_id"=>$data["idx_role_id"]))->row_array();
               if(!$cek){
                   $this->db->insert("tbl_user_policy",$data);
               }else{	
                    if($data["is_read"]==0)unset($data["is_read"]);
                    if($data["is_update"]==0)unset($data["is_update"]);
                    if($data["is_delete"]==0)unset($data["is_delete"]);
                    if($data["is_create"]==0)unset($data["is_create"]);
                    $this->db->update('tbl_user_policy',$data,array("tbl_function_id"=>$data["tbl_function_id"],"tbl_module_id"=>$data["tbl_module_id"],"idx_role_id"=>$data["idx_role_id"]));
               }
            }
            /*if($this->db->trans_status() == false){
                $this->db->trans_rollback();
                return "Data not saved";                
            }else{
                return $this->db->trans_commit();
            }*/
            
        }
        
        function deleteData($type){
            $id = $this->input->post("id");
            $table="";
            $this->db->trans_begin();
            switch ($type) {
                case "user_list":
                    $table = "tbl_user";
                    break;
                case "user_level":
                    $table="idx_user_level";
                    break;
                case "user_group":
                    $table="idx_user_group";
                    break;
                case "app_functions":
                    $table="tbl_user_module_functions";
                    break;
                case "app_modules":
                    $table="tbl_user_modules";
                    break;
                case "user_role":
                    $table="idx_user_role";
                    break;
            }
            $this->db->delete($table,array("id"=>$id));
            if($this->db->trans_status()==false){
                echo "Data Not Saved";
                return $this->db->trans_rollback();
            }else{
                return $this->db->trans_commit();
            }
        }
        
        function saveData($type,$param=""){
            $post = array();
            foreach($_POST as $k=>$v){
                $post[$k]=  $this->input->post($k);
            }
            echo $this->msetup->saveData($type,$post);
        }
        function getData($param){
            echo $this->msetup->getdata($param);
        }
        function combobox($type, $param="", $balikan=''){
		$this->load->helper('main_helper');
		$optTemp = "";
		if($param != ""){
			$p2 = $param;
		}else{
			$p2 = "";
		}
		
		if($balikan == ""){
			$balikan = $this->input->post('balikan');
		}else{
			$balikan = $balikan;
		}
		
		$param2 = "";
		$data = $this->msetup->getcombo($type, $param, $param2);
			
		$optTemp .= "<option value=''>-- Pilih --</option>";			
		foreach($data as $v=>$k){
			if($p2 == $k['code']){
				$optTemp .= "<option selected value='".$k['code']."'>".$k['value']."</option>";
			}else{
				$optTemp .= "<option value='".$k['code']."'>".$k['value']."</option>";
			}
		}
		
		if($balikan == 'echo'){
			echo $optTemp;
		}elseif($balikan == 'return'){
			return $optTemp;
		}
		
	}
}
